Page({
  data: {
    currentTab: 'all',
    products: [
      // 特色美食
      {
        id: 1,
        name: '阳朔特色米粉',
        description: '传统手工制作，选用优质大米，口感爽滑，搭配秘制酱料',
        originalPrice: 35,
        discountedPrice: 28,
        rating: 4.8,
        category: 'food',
        image: '/images/banner1.png'
      },
      {
        id: 2,
        name: '阳朔桂花糕',
        description: '精选当地桂花，传统工艺制作，香甜软糯，回味悠长',
        originalPrice: 58,
        discountedPrice: 45,
        rating: 4.7,
        category: 'food',
        image: '/images/banner1.png'
      },
      {
        id: 3,
        name: '阳朔辣椒酱',
        description: '传统秘制配方，香辣可口，下饭佐餐必备，本地特色调味品',
        originalPrice: 48,
        discountedPrice: 38,
        rating: 4.6,
        category: 'food',
        image: '/images/banner1.png'
      },

      // 手工艺品
      {
        id: 4,
        name: '阳朔竹编工艺品',
        description: '传统手工编织，天然竹材环保实用，家居装饰佳品',
        originalPrice: 158,
        discountedPrice: 128,
        rating: 4.9,
        category: 'craft',
        image: '/images/banner1.png'
      },
      {
        id: 5,
        name: '阳朔民族刺绣',
        description: '少数民族传统工艺，图案精美，色彩鲜艳，家居装饰佳品',
        originalPrice: 198,
        discountedPrice: 168,
        rating: 4.8,
        category: 'craft',
        image: '/images/banner1.png'
      },

      // 文化纪念品
      {
        id: 6,
        name: '阳朔山水画',
        description: '当地艺术家原创作品，展现漓江风光与人文风情',
        originalPrice: 368,
        discountedPrice: 298,
        rating: 4.9,
        category: 'souvenir',
        image: '/images/banner1.png'
      }
    ]
  },

  onLoad() {
    this.formatRatings();
    this.filterProducts(this.data.currentTab);
  },

  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({ currentTab: tab });
    this.filterProducts(tab);
  },

  // 过滤商品
  filterProducts(tab) {
    let filtered = [];

    if (tab === 'all') {
      filtered = this.data.products;
    } else {
      filtered = this.data.products.filter(p => p.category === tab);
    }

    this.setData({ filteredProducts: filtered });
  },

  // 格式化星级评分
  formatRatings() {
    const products = this.data.products.map(item => ({
      ...item,
      ratingArray: Array(Math.floor(item.rating)).fill(1)
    }));

    this.setData({ products });
  },

  buyProduct(e) {
    const productId = e.currentTarget.dataset.id;
    wx.showToast({ title: '已加入购物车', icon: 'success' });
    // 可扩展跳转到详情页或结算页
  }
});